

use crate::data::physics;
use crate::data::world2d::*;


#[cfg(test)]
mod tests
{
    use super::*;
    use ::rand::Rng;
    use crate::data::math::*;
    use super::super::draw_for_test::*;
    use ::macroquad::prelude::*;

    #[macroquad::test]
    async fn test_draw_physics()
    {
        let cp = CollisionPipeline::new();
        let mut pp = PhysicsPipeline::new();
        
        pp.collision_pipeline = cp;

        let mut body_list = physics::BodyList::new();
        for _ in 0..100
        {
            let mut rng = ::rand::thread_rng();
            let mut body = physics::Body::default();
            body.supported = false;
            body.mass = rng.gen_range(0.1..10.0);
            body.roughness = rng.gen_range(1.0..5.0);
            body.position.x = rng.gen_range(100.0..700.0);
            body.position.y = rng.gen_range(0.0..200.0);
            body.shape.r#type = physics::ShapeType::Rectangle(10.0, 10.0);
            body_list.push(body);
        }

        let floor = &mut body_list[0];
        floor.mass = 0.0;
        floor.position = Vector2 { x: 50.0, y: 500.0 };
        floor.shape.r#type = physics::ShapeType::Rectangle(700.0, 50.0);

        pp.set_body_list(body_list);
        pp.set_g(9.8);

        let mut rng = ::rand::thread_rng();
        let mut start = ::std::time::Instant::now();
        let mut timedelta: f64 = start.elapsed().as_secs_f64();
        loop {
            pp.update(timedelta);

            clear_background(BLACK);
            let body_list = pp.get_body_list_mut();
            for b in body_list
            {
                //println!("{:#?}", b);
                b.force.x += rng.gen_range(-0.1..0.1);
                let position = b.get_shape_position();
                let r = if let physics::ShapePosition::Rectangle(r) = position {
                    r
                } else { panic!(); };
                math::draw_rectangle(
                    r,
                    Color::new(
                        rng.gen_range(0.0..1.0),
                        1.0,
                        1.0,
                        1.0
                    )
                );
            }

            timedelta = start.elapsed().as_secs_f64();
            start = ::std::time::Instant::now();
            next_frame().await
        }
    }
}


